package com.liuf.di.configuration.kettle;

import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
import org.pentaho.di.repository.kdr.KettleDatabaseRepositoryMeta;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * @program: spring-cloud-liuf-master
 * @description: Kettle初始化-工厂模式
 * @author: Mr.Liu
 * @create: 2018-09-12 08:23
 * @码云： https://gitee.com/www.jiandan.com
 **/
@Component
public class KettleInitFactory {

    private static final Logger logger = LoggerFactory.getLogger(KettleInitFactory.class);

    /**kettle资源库连接参数*/
    @Value(value = "${kettle_repository_conn_name}")
    private String kettle_repository_conn_name;//资源库数据库名称
    @Value(value = "${kettle_repository_conn_type}")
    private String kettle_repository_conn_type;//资源库数据库类型
    @Value(value = "${kettle_repository_conn_access}")
    private String kettle_repository_conn_access;//资源库数据库连接方式
    @Value(value = "${kettle_repository_conn_host}")
    private String kettle_repository_conn_host;//资源库数据库IP
    @Value(value = "${kettle_repository_conn_db}")
    private String kettle_repository_conn_db;//资源库数据库数据库
    @Value(value = "${kettle_repository_conn_port}")
    private String kettle_repository_conn_port;//资源库数据库端口
    @Value(value = "${kettle_repository_conn_username}")
    private String kettle_repository_conn_username;//资源库数据库用户名
    @Value(value = "${kettle_repository_conn_pwd}")
    private String kettle_repository_conn_pwd;//资源库数据库密码

    /**资源库属性【可以配置多个资源库,当前版本不提供该功能】*/
    @Value(value = "${kettle_repository_id}")
    private String kettle_repository_id;//资源库ID
    @Value(value = "${kettle_repository_name}")
    private String kettle_repository_name;//资源库名称
    @Value(value = "${Kettle_repository_description}")
    private String Kettle_repository_description;//资源库备注


    private KettleDatabaseRepository repository;

    /** 初始加载kettle环境 */
    public KettleInitFactory(){
        try {
            logger.info("=================================初始化Kettle组件==================================");
            /**初始化kettle所有组件*/
            KettleEnvironment.init();
        } catch (KettleException e) {
            e.printStackTrace();
            logger.error("=================初始化失败======================");
        }
    }

    /**
     * 初始化加载资源库
     * @return
     */
    public KettleDatabaseRepository getRepositoryInit(){
        logger.info("=================================开始加载资源库==================================");
        if(repository == null){
            try {
                /**创建资源库对象*/
                repository = new KettleDatabaseRepository();
                /**资源库元对象,名称参数，id参数，描述等可以随便定义*/
                KettleDatabaseRepositoryMeta repInfo = new KettleDatabaseRepositoryMeta(kettle_repository_id, kettle_repository_name, Kettle_repository_description, this.getDatabaseMeta());
                repository.init(repInfo);
                repository.connect("admin", "admin");
                return repository;
            }catch (KettleException e){
                e.printStackTrace();
            }
        }
        return repository;
    }

    /**
     * 初始化资源库连接资源
     * @return  资源库数据资源库信息
     */
    public DatabaseMeta getDatabaseMeta(){
        logger.info("=================================加载资源库连接数据源信息==================================");
        DatabaseMeta databaseMeta = new DatabaseMeta(
                this.kettle_repository_name,
                this.kettle_repository_conn_type,
                this.kettle_repository_conn_access,
                this.kettle_repository_conn_host,
                this.kettle_repository_conn_db,
                this.kettle_repository_conn_port,
                this.kettle_repository_conn_username,
                this.kettle_repository_conn_pwd);
        return databaseMeta;
    }

    /**
     * 释放kettle资源资源
     * @param repository    数据库连接
     */
    public static void destroy(Repository repository){
        if(repository!=null){
            repository.disconnect();
            logger.info(repository.getName()+"资源库释放成功");
            repository = null;
        }
    }

}
